package com.wangjw.tuling.io;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by wangjianwen on 2018/12/4.
 */
public class MultiThreadServer {

    private int port;

    public MultiThreadServer(int port) {
        this.port = port;
    }

    private void startServer() {
        ServerSocket serverSocket = null;
        int i = 0;
        System.out.println("服务器在端口 " + port + " 等待客户请求......");

        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                Socket clientSocket = serverSocket.accept();
                new Thread(new ThreadedServerHandler(clientSocket, i++)).start();
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        new MultiThreadServer(8888).startServer();
    }
}
